#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <cstdlib>
#include <sys/wait.h>

// 17号信号 SIGCHLD

void handler(int sig)
{
    // std::cout << "子进程已退出" << std::endl;
    pid_t id;
    while ((id = waitpid(-1, nullptr, WNOHANG)) >= 0)
    {
        if (id > 0)
            std::cout << "wait child success: " << id << std::endl;
    }
    std::cout << "all child is quit!" << std::endl;
}

int main()
{
    signal(SIGCHLD, handler);
    pid_t pid = fork();
    if (pid == 0)
    {
        sleep(2);
        std::cout << "I am child, exit." << std::endl;
        exit(2);
    }
    while (1)
    {
        std::cout << "father proc is doing some thing!" << std::endl;
        sleep(1);
    }

    return 0;
}